
#include "heartbeat.h"

static const char *TAG = "heart_beat";
static const char *Null = "NULL";

void heart_Decode(void)
{
    extern volatile char heatrtbeat[500];                  //心跳
    extern const unsigned char grill_model;                //烤炉型号
    extern volatile unsigned char error_state;             //报错代码
    extern volatile unsigned char global_grill_state;      //烤炉状态
    extern volatile unsigned int global_set_temp;          //设置温度
    extern volatile signed int global_rtd0_temp;           //RTD温度
    extern volatile unsigned int global_rtd1_temp;         //RTD1温度
    extern volatile unsigned int global_rtd2_temp;         //RTD2温度
    extern volatile unsigned int global_rtd1_set_temp;     //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp;     //RTD2报警温度
    extern volatile unsigned long global_timmer_1_set;     //定时器1设定值
    extern volatile unsigned long global_timmer_2_set;     //定时器2设定值
    extern volatile unsigned long global_countdown_remain; //熄火程序剩余时间
    extern volatile unsigned long global_timmer_1_remain;  //定时器1剩余
    extern volatile unsigned long global_timmer_2_remain;  //定时器2剩余
    extern volatile unsigned int ID_first_four;            //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;              //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;             //MAC最后4位做识别
    extern volatile unsigned int firmware_number;          //固件版本号
    extern volatile unsigned char global_temp_unit;        //温度单位 0华氏度 1摄氏度f
    static unsigned int i = 0;                             //计数队列
    char *send_wifi_ssid = "";

    // if(AWS_Interface.Get_RTD_Handle != NULL) global_rtd0_temp = AWS_Interface.Get_RTD_Handle();
    // if(AWS_Interface.Get_ProbeA_Handle != NULL) global_rtd1_temp = AWS_Interface.Get_ProbeA_Handle();
    // if(AWS_Interface.Get_ProbeB_Handle != NULL) global_rtd2_temp = AWS_Interface.Get_ProbeB_Handle();
    // if(AWS_Interface.Get_RTDSettings_Handle != NULL) global_set_temp = AWS_Interface.Get_RTDSettings_Handle();
    // if(AWS_Interface.Get_ProbeASettings_Handle != NULL) global_rtd1_set_temp = AWS_Interface.Get_ProbeASettings_Handle();
    // if(AWS_Interface.Get_ProbeBSettings_Handle != NULL) global_rtd2_set_temp = AWS_Interface.Get_ProbeBSettings_Handle();
    // if(AWS_Interface.Get_Timer1Set_Handle != NULL) global_timmer_1_set = AWS_Interface.Get_Timer1Set_Handle();
    // if(AWS_Interface.Get_Timer2Set_Handle != NULL) global_timmer_2_set = AWS_Interface.Get_Timer2Set_Handle();
    // if(AWS_Interface.Get_Timer1_Handle != NULL) global_timmer_1_remain = AWS_Interface.Get_Timer1_Handle();
    // if(AWS_Interface.Get_Timer2_Handle != NULL) global_timmer_2_remain = AWS_Interface.Get_Timer2_Handle();
    // if(AWS_Interface.Get_Shutoff != NULL) global_countdown_remain = AWS_Interface.Get_Shutoff();

    // if(AWS_Interface.Get_State_Error != NULL) error_state = AWS_Interface.Get_State_Error();
    // if(AWS_Interface.Get_State_Grill != NULL) global_grill_state = AWS_Interface.Get_State_Grill();
    // if(AWS_Interface.Get_Unit_Grill != NULL) global_temp_unit = AWS_Interface.Get_Unit_Grill();
    
    // if(AWS_Interface.Get_State_Grill != NULL) global_grill_state = AWS_Interface.Get_State_Grill();
    // sprintf(heatrtbeat, "{\"ID\":\"%04x%04x%04x\",\"firmware\":%d,\"state\":%d,\"countdown\":%ld,\"rtd0_set\":%d,\"rtd1_set\":%d,\"rtd2_set\":%d,\"rtd0_temp\":%d,\"rtd1_temp\":%d,\"rtd2_temp\":%d,\"tmr1_set\":%ld,\"tmr2_set\":%ld,\"tmr1_rem\":%ld,\"tmr2_rem\":%ld,\"unit\":%d,\"err\":%d,\"ssid\":\"%s\",\"squ\":%d,\"menu\":%d,\"ota\":%d}", ID_first_four, ID_mid_four, ID_last_four, firmware_number, global_grill_state, global_countdown_remain, global_set_temp, global_rtd1_set_temp, global_rtd2_set_temp, global_rtd0_temp, global_rtd1_temp, global_rtd2_temp, global_timmer_1_set, global_timmer_2_set, global_timmer_1_remain, global_timmer_2_remain, global_temp_unit, error_state, send_wifi_ssid, i++,Cook_Menu,OTA_percentage);
}

void heart_beat_task(void *pvParameters)
{
    extern volatile char heatrtbeat[500];                  //心跳
    extern const unsigned char grill_model;                //烤炉型号
    extern volatile unsigned char error_state;             //报错代码
    extern volatile unsigned char global_grill_state;      //烤炉状态
    extern volatile unsigned int global_set_temp;          //设置温度
    extern volatile signed int global_rtd0_temp;           //RTD温度
    extern volatile unsigned int global_rtd1_temp;         //RTD1温度
    extern volatile unsigned int global_rtd2_temp;         //RTD2温度
    extern volatile unsigned int global_rtd1_set_temp;     //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp;     //RTD2报警温度
    extern volatile unsigned long global_timmer_1_set;     //定时器1设定值
    extern volatile unsigned long global_timmer_2_set;     //定时器2设定值
    extern volatile unsigned long global_countdown_remain; //熄火程序剩余时间
    extern volatile unsigned long global_timmer_1_remain;  //定时器1剩余
    extern volatile unsigned long global_timmer_2_remain;  //定时器2剩余
    extern volatile unsigned int ID_first_four;            //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;              //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;             //MAC最后4位做识别
    extern volatile unsigned int firmware_number;          //固件版本号
    extern volatile unsigned char global_temp_unit;        //温度单位 0华氏度 1摄氏度f
    static unsigned int i = 0;                             //计数队列
    char *send_wifi_ssid = "";
    g_wifi_ssid = Null;
    while (1)
    {
        // if(AWS_Interface.Get_RTD_Handle != NULL) global_rtd0_temp = AWS_Interface.Get_RTD_Handle();
        // if(AWS_Interface.Get_ProbeA_Handle != NULL) global_rtd1_temp = AWS_Interface.Get_ProbeA_Handle();
        // if(AWS_Interface.Get_ProbeB_Handle != NULL) global_rtd2_temp = AWS_Interface.Get_ProbeB_Handle();
        // if(AWS_Interface.Get_RTDSettings_Handle != NULL) global_set_temp = AWS_Interface.Get_RTDSettings_Handle();
        // if(AWS_Interface.Get_ProbeASettings_Handle != NULL) global_rtd1_set_temp = AWS_Interface.Get_ProbeASettings_Handle();
        // if(AWS_Interface.Get_ProbeBSettings_Handle != NULL) global_rtd2_set_temp = AWS_Interface.Get_ProbeBSettings_Handle();
        // if(AWS_Interface.Get_Timer1Set_Handle != NULL) global_timmer_1_set = AWS_Interface.Get_Timer1Set_Handle();
        // if(AWS_Interface.Get_Timer2Set_Handle != NULL) global_timmer_2_set = AWS_Interface.Get_Timer2Set_Handle();
        // if(AWS_Interface.Get_Timer1_Handle != NULL) global_timmer_1_remain = AWS_Interface.Get_Timer1_Handle();
        // if(AWS_Interface.Get_Timer2_Handle != NULL) global_timmer_2_remain = AWS_Interface.Get_Timer2_Handle();
        // if(AWS_Interface.Get_Shutoff != NULL) global_countdown_remain = AWS_Interface.Get_Shutoff();

        // if(AWS_Interface.Get_State_Error != NULL) error_state = AWS_Interface.Get_State_Error();
        // if(AWS_Interface.Get_State_Grill != NULL) global_grill_state = AWS_Interface.Get_State_Grill();
        // if(AWS_Interface.Get_Unit_Grill != NULL) global_temp_unit = AWS_Interface.Get_Unit_Grill();
       
        if (wireless_state.station_state == disconnect)
        {
            send_wifi_ssid = Null;
        }
        else
        {
            send_wifi_ssid = g_wifi_ssid;
        }
        // if(AWS_Interface.Get_State_Grill != NULL) global_grill_state = AWS_Interface.Get_State_Grill();
        // sprintf(heatrtbeat, "{\"ID\":\"%04x%04x%04x\",\"firmware\":%d,\"state\":%d,\"countdown\":%ld,\"rtd0_set\":%d,\"rtd1_set\":%d,\"rtd2_set\":%d,\"rtd0_temp\":%d,\"rtd1_temp\":%d,\"rtd2_temp\":%d,\"tmr1_set\":%ld,\"tmr2_set\":%ld,\"tmr1_rem\":%ld,\"tmr2_rem\":%ld,\"unit\":%d,\"err\":%d,\"ssid\":\"%s\",\"squ\":%d,\"menu\":%d,\"ota\":%d}", ID_first_four, ID_mid_four, ID_last_four, firmware_number, global_grill_state, global_countdown_remain, global_set_temp, global_rtd1_set_temp, global_rtd2_set_temp, global_rtd0_temp, global_rtd1_temp, global_rtd2_temp, global_timmer_1_set, global_timmer_2_set, global_timmer_1_remain, global_timmer_2_remain, global_temp_unit, error_state, send_wifi_ssid, i++,Cook_Menu,OTA_percentage);
        
        // //ESP_LOGI(TAG, "update heart beat...");
        vTaskDelay(1000 / portTICK_RATE_MS); //任务延迟
    }
}

esp_err_t heart_beat_init(void)
{
    //ESP_LOGI(TAG, "start heart beat task creat...");
    xTaskCreate(heart_beat_task, "heart_beat_task", 2048, NULL, 5, NULL);
    //ESP_LOGI(TAG, "heart beat task created");
    return ESP_OK;
}